home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / Libraries / DCLAP 6d / dclap6d / SeqPups / appsrc / autoseq.src / ReadABITag.C < prev    next >
C/C++ Source or Header  |  1996-07-05  |  4KB  |  189 lines

  1. #include    <stdlib.h>
  2. #include    <stdio.h>
  3. #include    <string.h>
  4. #include    <iostream.h>
  5. #include    "RInclude.H"
  6. #include    "RInlines.H"
  7.  
  8. typedef ushort int2;                        // Two byte short assumption
  9. typedef ulong  int4;                        // Four byte long assumption
  10. const size_t INDEXPO        = 26;            // offset of offset to index
  11. const int    INDEX_ENTRY_LENGTH= 28;
  12.  
  13. char    FatalString[200];
  14. void    Fatal(char* error)
  15.     { cerr << "disect: FATAL: " << error << endl; exit(1); }
  16.  
  17. //    ============================================================================
  18. //    readABIInt2
  19. //    ========================================|===================================
  20. bool
  21. readABIInt2(
  22.     FILE*    fp,
  23.     int2*    i2)
  24.     {
  25.     unsigned char buf[sizeof(int2)];
  26.  
  27.     if (fread(buf, sizeof(buf), 1, fp) != 1) return (FALSE);
  28.     *i2 = (int2)
  29.         (((unsigned short)buf[1]) +
  30.          ((unsigned short)buf[0]<<8));
  31.     return (TRUE);
  32.     }
  33.  
  34. //    ============================================================================
  35. //    readABIInt4
  36. //    ========================================|===================================
  37. bool
  38. readABIInt4(
  39.     FILE*    fp,
  40.     int4*    i4)
  41.     {
  42.     unsigned char buf[sizeof(int4)];
  43.  
  44.     if (fread(buf, sizeof(buf), 1, fp) != 1) return (FALSE);
  45.     *i4 = (int4)
  46.         (((unsigned long)buf[3]) +
  47.          ((unsigned long)buf[2]<<8) +
  48.          ((unsigned long)buf[1]<<16) +
  49.          ((unsigned long)buf[0]<<24));
  50.     return (TRUE);
  51.     }
  52.  
  53. //    ============================================================================
  54. //    getIndexEntryLW
  55. //    -  -  -     -    -  -  -     -    -  -  -     -    -  -  -     -    -  -  -     -    -  -  -     -    -  -
  56. //    From the ABI results file connected to `fp' whose index starts at byte
  57. //    offset `indexO', return in `val' the `lw'th long word from the `count'th
  58. //    entry labeled `label'. The result indicates success.
  59. //    ========================================|===================================
  60. bool
  61. getIndexEntryLW1(
  62.     FILE*    fp,
  63.     long    indexO,
  64.     long    label,
  65.     long    count,
  66.     int        lw,
  67.     int4*    val)
  68.     {
  69.     int        entryNum=-1;
  70.     int        i;
  71.     int4    entryLabel, entryLw1;
  72.  
  73.     do
  74.         {
  75.         entryNum++;
  76.         cout << entryNum << ": ";
  77.         cout << 1;
  78.         if (fseek(fp, indexO+(entryNum*INDEX_ENTRY_LENGTH), SEEK_SET) != 0)
  79.             return FALSE;
  80.         cout << 2;
  81.         if (!readABIInt4(fp, &entryLabel))
  82.             return FALSE;
  83.         cout << 3;
  84.         if (!readABIInt4(fp, &entryLw1))
  85.             return FALSE;
  86.         cout << 4 << endl;
  87.         } while (!(entryLabel == label && entryLw1 == count));
  88.  
  89.         cout << 5;
  90.  
  91.     for(i=2; i<=lw; i++)
  92.         if (!readABIInt4(fp, val)) return FALSE;
  93.  
  94.         cout << 6;
  95.     return TRUE;
  96.     }
  97.  
  98. //    ============================================================================
  99. //    getIndexEntryW
  100. //    -  -  -     -    -  -  -     -    -  -  -     -    -  -  -     -    -  -  -     -    -  -  -     -    -  -
  101. //    From the ALF results file connected to `fp' whose index starts at byte
  102. //    offset `indexO', return in `val' the `lw'th     word (int2) from the entry
  103. //    labeled `label'. The result indicates success.
  104. //    ========================================|===================================
  105. bool
  106. getIndexEntryW(
  107.     FILE*    fp,
  108.     long    indexO,
  109.     long    label,
  110.     int        lw,
  111.     int2*    val)
  112.     {
  113.     int        entryNum=-1;
  114.     int        i;
  115.     int4    entryLabel;
  116.     int4    jval;
  117.  
  118.     do
  119.         {
  120.         entryNum++;
  121.         if (fseek(fp, indexO+(entryNum*INDEX_ENTRY_LENGTH), SEEK_SET) != 0)
  122.             return FALSE;
  123.         if (!readABIInt4(fp, &entryLabel))
  124.             return FALSE;
  125.         }
  126.         while (entryLabel != label);
  127.  
  128.  
  129.     for(i=2; i<lw; i++)
  130.         if (!readABIInt4(fp, &jval)) return FALSE;
  131.     
  132.     if (!readABIInt2(fp, val)) return FALSE;
  133.  
  134.     return TRUE;
  135.     }
  136.  
  137. const long DATAtag            = ((long) ((((('D'<<8)+'A')<<8)+'T')<<8)+'A');
  138. //
  139. // MAIN
  140. //
  141. int
  142. main(int argc, char* argv[])
  143.     {
  144.     FILE*    fp;
  145.     long    label = DATAtag; //Pack4Chars(argv[2]);
  146.     ulong    count = atol(argv[3]);
  147.     int4    indexO;
  148.     char    tag[5]={NULL};
  149.     int4    dummyVal;
  150.  
  151.     if (argc!=4)
  152.         {
  153.         cerr << "ABItag <filename> <tag> <which>" << endl;
  154.         exit(1);
  155.         }
  156.     
  157.     if (!(fp = fopen(argv[1],"rb")))
  158.         {
  159.         strcpy(FatalString,"couldn't open '");
  160.         strcpy(FatalString,argv[1]);
  161.         strcpy(FatalString,"'.");
  162.         Fatal(FatalString);
  163.         }
  164.  
  165.     // get the index offset
  166.     if ((fseek(fp, INDEXPO, SEEK_SET) != 0) || (!readABIInt4(fp, &indexO)))
  167.         Fatal("couldn't read indexO");
  168.  
  169.     cout << "FILE:   " << argv[1] << endl;
  170.     cout << "indexO: " << indexO << endl;
  171.     cout << "TAG:    " << Unpack4Chars(label,tag) << endl;
  172.  
  173.     cout << "count\t0\t1\t2\t3\t4\t5\t6" << endl;    
  174.     bool keepGoing = TRUE;
  175.     for(uint i=0 ; keepGoing ; i++)
  176.         {
  177.         cout << i << "\t";
  178.         for(uint j=0; keepGoing && (j<=5); j++)
  179.             if (!(getIndexEntryLW1(fp, indexO, DATAtag, i, j, &dummyVal)))
  180.                 keepGoing = FALSE;
  181.             else
  182.                 cout << dummyVal << "\t";
  183.         cout << endl;
  184.         }
  185.  
  186.     fclose(fp);
  187.     }
  188.  
  189.